<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/1
 * Time: 16:37
 */

namespace common\business\department;

use common\business\kingdee\OrgAdminApiHandler;
use common\models\kingdee\OrgAdmin;
use Yii;
use common\business\DbHandler;
use common\models\Department;
use common\result\ActionResult;

class DepartmentApiHandler extends DbHandler
{
    /**
     * @param $obj Department
     * @return ActionResult
     */
    public function createDepartment($obj){
        return $this->create($obj);
    }

    /**
     * @param Department $obj
     * @return ActionResult
     */
    public function updateDepartment($obj){
        $totalResult = new ActionResult();
        $oldObj = Department::findOne([
            'id' => $obj->id,
        ]);
        $selfResult = $this->update($obj, $oldObj, [
            'name',
            'number',
            'display_name',
            'layer',
            'is_company',
            'parent_id',
            'parent_number',
            'qywx_id',
            'parent_qywx_id',
            'order',
            'memo',
            'hr_id',
            'mdm_id',
            'parent_mdm_id',
            'kd_id',
            'parent_kd_id',
            'root',
            'lft',
            'rgt',
            'lvl',
            'icon',
            'icon_type',
            'active',
            'selected',
            'disabled',
            'readonly',
            'visible',
            'collapsed',
            'movable_u',
            'movable_d',
            'movable_l',
            'movable_l',
            'removable',
            'removable_all',
        ]);
//        还需要反写OrgAdmin的企业微信号
        $totalResult->addSubResult($selfResult);
        $orgApi = new OrgAdminApiHandler();
        $orgAdminInDb = OrgAdmin::findOne(['FNUMBER' => $obj->number]);
        if (!$orgAdminInDb){
            return $totalResult;
        }
        $orgAdminInDb->FCODE = strval($obj->qywx_id);
        $orgAdminResult = $orgApi->updateOrgAdmin($orgAdminInDb);
        $totalResult->addSubResult($orgAdminResult);
        return $totalResult;
    }

    /**
     * @param $obj Department
     * @return ActionResult
     */
    public function deleteDepartment($obj){
        return $this->delete($obj);
    }
}